<!--
File description:


The functionalities in this file are used throughout the entire program, yet were too generic to be implemented individually in the other scripts.  As such, this 
script serves as the go-to script for any future function implementations.  If one doesn't know where to implement a function, the default choice should be here.

This script contains the following generic functionalities:
	1) Executing a command on a remote server using pre-established SSH private keys.  Unable to use regular SSH due to a broken SVN version on the server we are working on.
	
	2) Set the contents of a txt file to store a file path needed as a ever-changing global variable throughout this program
	
	3) Parse the contents of a specific directory on the server into an array of arrays, which each filename is broken into sub-components (the sub arrays)

 -->

<?php

require_once "globals.php";

//This command executes a string passed command on the remote Multilab server, and returns the result
function executeRemoteCommand($command){
	//Make sure the command will have all that it needs in its PATH to perform the command
	putenv("PATH=/bin:/usr/local/bin:/usr/local/bin/csrepo;/usr/local/gnu/bin:/usr/openwin/bin:/usr/local/X11R6/bin:/usr/ccs/bin:/usr/bin:/usr/ucb:/etc:/usr/local/java/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mounts/u-zon-d2/ugrad/mtlank2/HTML/scripts:/usr/bin/expect:/usr/bin/X11/expect:/usr/share/man/man1/expect.1.gz:/usr/lib/x86_64-linux-gnu/libserf-1.so.1");
	//Attempt to include missing SVN linked library dependency, waiting on sysadmin to fix SVN 
	putenv("LD_LIBRARY_PATH=/lib:/usr/lib:/usr/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/libserf-1.so.1");
	//Execute the command on Multilab
	return shell_exec('2>&1 ssh mtlank2@penstemon.cs.engr.uky.edu "'.$command.'"');
}


//This command takes the ABSOLUTE (pwd) path of where the OCR files are located and stores this string in a file
function setOcrFilesPath($directoryLocation){
//Access our constant path from a txt file which stores the path for long term use outside of this script
file_put_contents("constants/ocrFilesPath.txt" ,$directoryLocation); 
}


/*This command stores all files in the logs directory into an array of arrays.  
The format of the containing arrays is [name][book][page][time]
*/
function parseLogFiles (){
	//Define for function scope
	global $locationOfLogFiles;
	//Will be used as an array of arrays
	$parsedLogFiles = array();

	//Obtain the list of log files and store them in an array. locationOfLogFiles is a global variable, found in globals.php
	$files = scandir($locationOfLogFiles);

	//Error check to see if we failed to load the log files into an array. Upon failure, return empty set
	if (!$files){
		echo "Failed to retreive log files.<br>";
		return  $parsedLogFiles;
	}
	//Upon successful loading of log files into a parseable array
	else{
		/*Each log file title is of the format "user~book~page~time".  The following for loop stores an array of arrays
		(ie a 2d array) in which each log file title is parsed for its info.  In other words, user,book,page,and time 
		will all be parsed into individual array elements.   We are essentially parsing the names of files here. 

		Example: We have a log file called "bob~greenEggsAndHam~5~2397829".  The contents of the file store an older
		version of the original page 5 of greenEggsAndHam before timestamp 2397829, afterwhich bob made a change. This
		function is simply concerned with parsing "bob~greenEggsAndHam~5~2397829" (and other log files like it) into 
		an array [bob][greenEggsAndHam][5][2397829].  An array of these types of arrays is what is return, or false
		upon failure.   
		*/
		for ($i = 2 ; $i < sizeof($files); $i++){
			array_push($parsedLogFiles, explode("~", $files[$i]));
		}
		//Now we return all information about each particular log file
		return $parsedLogFiles;
	}
}



/*
***For Professor Piwowarski: Below is my personal list of info/things/to-dos that I use while coding. It will be removed before going live***

TO-DO:  		
-remove merge conflicts with Scott
-fix up admin.php --SCOTT

-fix up user.php --SCOTT
-write a technical document about usage for finkel using his technical writing link
-do dictionary word list highlights


CHECKLIST FOR LIVE VERSION:
-go into sholem_login.php, there are 4 lines in two groupings in which you have to uncomment the test files "book" and "0016" and set their respective varilables to value in the comment beside these lines of code
-remove this TO-DO list

POSSIBLE and KNOWN BUGS:
-refresh bug on log in due to POST to itself
-Known: Suppose a regular user makes a change, then an admin makes a change to the same line before the admin approves the user change first.  
Now suppose the admin chooses to allow the user change.  The admin change will override the user change.  The user change will not take effect.

INFO:	
This the path to the actual OCR files:	
/mounts/u-al-d3/csfac/raphael/projects/ocr/SholemAleykhem

permissions: 
0=regular 1=auto-commit-person 2=admin
using 1/0 for boolean login, not true/false.  (shouldnt matter immediately)

The Dev Repo: 
https://499sholem.googlecode.com/svn/trunk/
uz8Bq2us7UW5

*/





?>